<template>
  <div id="add_paramter" class="Row_content" style="width:450px;">
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="110">
      <FormItem label="参数名称：" prop="paramterName">
        <Input v-model="formValidate.paramterName" placeholder="请输入" class='input-wid' :maxlength='10' @on-keyup="filterName('paramterName')" @on-blur="filterName('paramterName')"></Input>
      </FormItem>
      <FormItem label="属性名称：" prop="classifyName">
        <Select v-model="formValidate.classifyName" class='input-wid'>
          <Option v-for="(item, index) in classifyNameList" :value="item.packageId" :key="index">{{item.packageName}}</Option>
        </Select>
      </FormItem>
      <FormItem label="是否必填信息：" prop="msgShow">
        <RadioGroup v-model="formValidate.msgShow" size="large">
          <Radio label="1">是</Radio>
          <Radio label="0" style="margin-left:40px">否</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="是否显示：" prop="isShow">
        <RadioGroup v-model="formValidate.isShow" size="large">
          <Radio label="1">显示</Radio>
          <Radio label="0" style="margin-left:40px">隐藏</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="参数录入方式：" prop="selectWay">
        <RadioGroup v-model="formValidate.selectWay" size="large" @on-change='onChange'>
          <Radio label="0">手动</Radio>
          <Radio label="1" style="margin-left:25px">单选</Radio>
          <Radio label="2" style="margin-left:25px">多选</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="参数列表：" prop="paramList" v-if="formValidate.selectWay === '1' || formValidate.selectWay === '2'">
        <!-- <Input v-model="formValidate.selectParamter" type="textarea" :autosize="{ minRows: 4, maxRows: 8 }" class="Row_content_input" style='width:320px' @on-change='changeList'></Input>
        <p style="color: #808695;">一行代表一个可选值</p> -->
        <div class="select-param">
          <Input v-model="selectParamter" style="border: 0" placeholder="请输入需要录入的参数选项" :maxlength='10' :disabled="formValidate.paramList.length > 9 ? true: false">
            <!-- <span slot="append" @click='submitParam'>提交</span> -->
          </Input>
          <Button @click='submitParam' style='background-color: #f8f8f9;border: 0' :disabled="formValidate.paramList.length > 9 ? true: false">提交</Button>
        </div>
        <!-- <div style="margin-top:10px">
          <Button type="info" v-for="(item,idx) in paramList" :key='idx' style="margin: 5px;border-radius: 30px;">
            <span>{{item.value}}</span>
            <span style="margin-left: 5px"><Icon type="ios-close" size='20' /></span>
          </Button>
        </div> -->
        <Table border :columns="columns" :data="formValidate.paramList" style="margin-top: 15px;width: 330px;" class="table-param">
          <template slot-scope="{ row,index }" slot="action">
            <i class="iconsys icondel" title="删除" @click="delParam(row, index)"></i>
          </template>
        </Table>
      </FormItem>
      <FormItem style="margin-top: 36px">
        <Button type="primary" class="btn" @click="handleSubmit">确定</Button>
        <Button class="btn" style="margin-left: 24px" @click="handleReset">取消</Button>
      </FormItem>
    </Form>
  </div>
</template>

<script>
import { getAttributeList, addParams } from '@/api/commondity/index'
import { routeEqual } from '@/libs/util'
export default {
  name: 'add_paramter',
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (vm.formValidate.classifyName !== to.query.id) {
        vm.formValidate.classifyName = to.query.id
        vm.paramId = to.query.id
        vm.getList()
      }
    })
  },
  data () {
    return {
      selectParamter: '',
      columns: [
        {
          title: '录入参数值',
          key: 'value'
        },
        {
          title: '操作',
          slot: 'action',
          width: 125
        }
      ],
      paramId: '',
      submitLock: false,
      classifyNameList: [],
      formValidate: {
        paramterName: '',
        classifyName: '',
        msgShow: '',
        isShow: '',
        selectWay: '',
        paramList: []
      },
      ruleValidate: {
        paramterName: { required: true, message: '请填写参数名称', trigger: 'change' },
        classifyName: { required: true, message: '请填写属性名称', trigger: 'change' },
        msgShow: { required: true, message: '请选择是否必填信息', trigger: 'change' },
        isShow: { required: true, message: '请选择是否显示', trigger: 'change' },
        selectWay: { required: true, message: '请选择参数录入方式', trigger: 'change' },
        paramList: {
          required: true,
          type: 'array',
          // trigger: 'blur',
          min: 1,
          message: '请录入参数选项'
        }
      }
    }
  },
  methods: {
    filterName (params) {
      this.formValidate[params] = this.formValidate[params].replace(/[^\u4e00-\u9fa5]/g, '')
    },
    delParam (row, index) {
      this.formValidate.paramList.splice(index, 1)
    },
    submitParam () {
      if (this.selectParamter) {
        this.formValidate.paramList.push({ value: this.selectParamter })
        this.selectParamter = ''
      } else {
        this.$Message.error('请录入内容后提交')
      }
    },
    handleSubmit () {
      this.$refs.formValidate.validate(valid => {
        if (valid) {
          let data = {
            merchantId: this.$store.state.user.merchantId,
            packageId: this.formValidate.classifyName,
            propName: this.formValidate.paramterName,
            inputType: this.formValidate.selectWay,
            notNull: this.formValidate.msgShow,
            searchStatus: this.formValidate.isShow,
            creator: this.$store.state.user.userId,
            creatorName: this.$store.state.user.userName
          }
          if (this.formValidate.selectWay === '1' || this.formValidate.selectWay === '2') {
            let arr = []
            this.formValidate.paramList.forEach(item => {
              arr.push(item.value)
            })
            data.propValues = arr
          }
          if (this.submitLock) {
            return
          }
          this.submitLock = true
          addParams(data).then(res => {
            this.submitLock = false
            const result = res.data || {}
            if (result.status === 200) {
              this.$Message.success('新增成功!')
              this.handleReset()
            } else {
              this.$Message.error(`${result.error}`)
            }
          }).catch(err => {
            this.submitLock = false
            this.$Message.error(`${err}`)
          })
        }
      })
    },
    handleReset () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'paramter_list',
          params: { id: this.paramId }
        })
      })
    },
    onChange () {},
    changeList (event) {
      this.formValidate.selectParamter = this.formValidate.selectParamter.replace(/ /g, '')
    },
    // 获取属性列表
    getList () {
      getAttributeList({
        merchantId: this.$store.state.user.merchantId
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.classifyNameList = result.data
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
      })
    }
  },
  mounted () {
    this.getList()
    this.formValidate.classifyName = this.$route.query.id
    this.paramId = this.$route.query.id
  }
}
</script>

<style lang="less">
#add_paramter{
  .input-wid{ width: 260px;}
  .btn{ width: 100px;}
  .select-param{
    display: flex;
    border: 1px solid #dcdee2;
    border-radius: 4px;
    width: 260px;
    overflow: hidden;
    .ivu-input{
      border: 0 !important;
      border-radius: 0 !important;
      /* border-top-left-radius: 4px;
      border-bottom-left-radius: 4px; */
    }
    .ivu-btn{
      border-radius: 0 !important;
      border-left: 1px solid #dcdee2 !important;
    }
  }
  .table-param{
    .ivu-table th, .ivu-table td{
      height: 37px !important;
    }
  }
}
</style>
